package com.vf.admin.client.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.vf.admin.client.common.ClientBaseController;
import com.vf.admin.client.common.ClientConstants;
import com.vf.admin.client.service.AppAuthUserService;
import com.vf.admin.client.service.AppUserCollectionService;
import com.vf.admin.common.entity.Result;
import com.vf.admin.miaodong.entity.AppAuthUser;
import com.vf.admin.miaodong.entity.AppUserCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/app/collection")
public class AppUserCollectionController extends ClientBaseController {

    @Autowired
    AppUserCollectionService collectionService;

    @Autowired
    private AppAuthUserService appAuthUserService;

    @RequestMapping("/saveOrUpdate")
    public Result saveOrUpdate(AppUserCollection params) {

        if(ObjectUtil.isNull(params)){
            return errorJSON("参数不能为空！");
        }
        Integer userId = getUserId();
        AppAuthUser appNameAuthUser = appAuthUserService.infoByUserAndNameB(userId, ClientConstants.UserAuth.REAL_NAME_AUTH);
        Boolean realNameAuth = ObjectUtil.isNotNull(appNameAuthUser) && ObjectUtil.isNotNull(appNameAuthUser.getStatus()) && appNameAuthUser.getStatus().equals(2);
        if (!realNameAuth) {
            return errorJSON("请先实名认证！");
        }

        if(StrUtil.isNotBlank(params.getAliphonenum())&&!params.getAliname().equals(appNameAuthUser.getField1())){
            return errorJSON("支付宝名称必须与实名认证一致！");
        }

        if(StrUtil.isNotBlank(params.getBankcardname())&&!params.getBankcardname().equals(appNameAuthUser.getField1())){
            return errorJSON("银行卡姓名必须与实名认证一致！");
        }

        AppUserCollection userCollection = collectionService.findByUserId(userId);
        if (userCollection == null) {
            params.setUserid(userId);
            collectionService.saveOrUpdate(params);
        } else {
            userCollection.setBankcard(params.getBankcard());
            userCollection.setBankcardname(params.getBankcardname());
            userCollection.setAliphonenum(params.getAliphonenum());
            userCollection.setAliname(params.getAliname());
            collectionService.saveOrUpdate(userCollection);
        }
        return successJSON();
    }

    @RequestMapping("/get")
    public Result get() {
        Integer userId = getUserId();
        AppUserCollection userCollection = collectionService.findByUserIdOrNULL(userId);
        return sucessJSON(userCollection);
    }

}
